﻿Imports System.IO
Imports System.Data.SqlClient
Imports System.Net

Public Class viewitem
    Public MainForm As Form1
    Public FtpFile As String
    Public Upid As String
    Public Description As String
    Private Sub viewitem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = False
        Try
            If FtpFile.Contains(".pdf") Then
                PictureBox1.Image = My.Resources.pdf
            End If

            Dim t As New Threading.Thread(AddressOf getfilesize)
            t.Start()
        Catch ex As Exception
        End Try
    End Sub
    Sub getfilesize()
        Dim AdditionalText As String = "..."
        Try
            lblinfo.Text = lblinfo.Text & AdditionalText

            Dim FtpRequest As Net.FtpWebRequest
            Dim FtpResponse As Net.FtpWebResponse
            FtpRequest = Net.FtpWebRequest.Create(FtpFile)
            FtpRequest.Credentials = New NetworkCredential(My.Settings.ftpusername, My.Settings.ftppassword)
            FtpRequest.KeepAlive = False
            FtpRequest.UseBinary = True

            FtpRequest.Method = Net.WebRequestMethods.Ftp.GetFileSize
            FtpRequest.Proxy = Nothing
            FtpResponse = FtpRequest.GetResponse
            Dim filesize As Double
            filesize = FtpResponse.ContentLength
            AdditionalText = Math.Round(filesize / 1024) & "KB"
            lblinfo.Text = lblinfo.Text.Replace("...", "") & AdditionalText
        Catch ex As Exception
            AdditionalText = ""
            lblinfo.Text = lblinfo.Text.Replace("...", "") & AdditionalText
        End Try
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click

    End Sub

    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
        Dim a
        a = MsgBox("Do you want to permanently remove '" & lbltitle.Text & "'?", MsgBoxStyle.YesNo, "Delete")
        If a = vbYes Then
            Dim con As New Odbc.OdbcConnection
            con.ConnectionString = My.Settings.con
            Dim sqlcommand As String = "DELETE FROM `records` WHERE upid = '" & Upid & "'"
            Dim cmd As New Odbc.OdbcCommand(sqlcommand, con)
            Try
                con.Open()
                cmd.ExecuteNonQuery()
                Dim delftp As New Threading.Thread(AddressOf DeleteFtpItem)
                delftp.Start()

                Me.Dispose()

                MainForm.color1 = 1
                MainForm.ChangeColor()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            con.Close()
        End If
        Try
            MainForm.color1 = 1
            MainForm.ChangeColor()
        Catch ex As Exception

        End Try
    End Sub
    Sub DeleteFtpItem()
        Try
            ' File.Delete(ServerFile.FullName)
            Dim FtpRequest As Net.FtpWebRequest
            Dim FtpResponse As Net.FtpWebResponse
            FtpRequest = Net.FtpWebRequest.Create(FtpFile)
            FtpRequest.Credentials = New NetworkCredential(My.Settings.ftpusername, My.Settings.ftppassword)
            FtpRequest.KeepAlive = False
            FtpRequest.UseBinary = True

            FtpRequest.Method = Net.WebRequestMethods.Ftp.DeleteFile
            FtpRequest.Proxy = Nothing
            FtpResponse = FtpRequest.GetResponse

        Catch ex As Exception

        End Try
    End Sub


    Dim rowClicked As Integer
    Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Left Or e.Button = Windows.Forms.MouseButtons.Right Then
            'rowClicked = DataGridView1.HitTest(e.Location.X, e.Location.Y).RowIndex
            ContextMenuStrip1.Show(PictureBox1, e.Location)
        End If
    End Sub

    Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs)
        'Try
        '    Process.Start(ServerFile.FullName)
        'Catch ex As Exception
        '    MsgBox(ex.Message)
        'End Try
    End Sub

    Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
        Try
            Dim save As New SaveFileDialog
            save.Filter = "PDF Files (*.pdf) |*.pdf"
            save.FilterIndex = 2
            save.RestoreDirectory = True
            If save.ShowDialog() = DialogResult.OK Then
                ' File.Copy(ServerFile.FullName, save.FileName, True)
                Dim ftp As FtpWebRequest = CType(FtpWebRequest.Create(FtpFile), FtpWebRequest)
                'Console.WriteLine("Downloading: " & FtpFile)
                ftp.Credentials = New NetworkCredential(My.Settings.ftpusername, My.Settings.ftppassword)
                ftp.KeepAlive = False
                ftp.UseBinary = True
                ftp.Method = WebRequestMethods.Ftp.DownloadFile
                Using FtpResponse As FtpWebResponse = CType(ftp.GetResponse, FtpWebResponse)
                    Using ResponseStream As IO.Stream = FtpResponse.GetResponseStream

                        Using fs As New IO.FileStream(save.FileName, FileMode.Create)
                            Dim buffer(2047) As Byte
                            Dim read As Integer = 0
                            Do
                                read = ResponseStream.Read(buffer, 0, buffer.Length)
                                fs.Write(buffer, 0, read)
                                Console.Write(".")
                            Loop Until read = 0
                            ResponseStream.Close()
                            fs.Flush()
                            fs.Close()
                        End Using
                        ResponseStream.Close()
                       
                    End Using
                End Using

                Try
                    Dim a = MsgBox("Do you want to open this document?", MsgBoxStyle.YesNo)
                    If a = vbYes Then
                        Dim Path As New DirectoryInfo(System.IO.Path.GetDirectoryName(save.FileName))
                        Path.Refresh()
                        Shell("explorer " & save.FileName, AppWinStyle.MaximizedFocus, True, 1000)
                    End If
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try

            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public LabelVendor As String
    Public type As String
    Public issued As String
    Public DEsc As String
    Public ref1 As String
    Public ref2 As String
    Public check1 As String

    Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click
        Dim InfoBox As New Info
        InfoBox.LabelTitle.Text = lbltitle.Text
        InfoBox.LabelVendor.Text = LabelVendor
        InfoBox.LabelType.Text = type
        InfoBox.LabelIssued.Text = issued
        InfoBox.TextBoxDescription.Text = DEsc
        InfoBox.lblref1.Text = ref1
        InfoBox.lblref2.Text = ref2
        InfoBox.lblcheck.Text = check1
        InfoBox.ShowDialog()
        Try
            MainForm.color1 = 1
            MainForm.ChangeColor()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
        'MainForm.ComboBox1.Text = lbltitle.Text
        Dim EditFOrm As New edit
        'EditFOrm.Parent = Me
        EditFOrm.txttitle.Text = lbltitle.Text
        EditFOrm.cmbvendor.Text = LabelVendor
        EditFOrm.dateissued.Value = issued
        EditFOrm.TextBoxDescription.Text = DEsc
        EditFOrm.UpID = Upid
        EditFOrm.txtref1.Text = ref1
        EditFOrm.txtref2.Text = ref2
        EditFOrm.txtcheck1.Text = check1
        For Each itm In MainForm.Doctypes
            EditFOrm.cmbdocument.Items.Add(itm)
        Next
        EditFOrm.cmbdocument.Text = type

        If MainForm.AutoCompleteCollectionVendor.Count <> 0 Then
            EditFOrm.cmbvendor.AutoCompleteCustomSource = MainForm.AutoCompleteCollectionVendor
        End If
        

        EditFOrm.ShowDialog()

        'MainForm.LoadItems(MainForm.ComboBox1.Text)
        ''cmbvendor

        'Try
        '    MainForm.color1 = 1
        '    MainForm.ChangeColor()
        'Catch ex As Exception

        'End Try
        Try
            GetEditedINfo(Upid)
        Catch ex As Exception

        End Try

    End Sub

    'Get Edited Info

    Sub GetEditedINfo(ItemID As String)
        Dim SQLCommand As String = "Select * FROM `records` where `upid` = '" & ItemID & "';"

        Dim con As New Odbc.OdbcConnection
        Dim reader As Odbc.OdbcDataReader
        con.ConnectionString = My.Settings.con
        Dim cmd As New Odbc.OdbcCommand(SQLCommand, con)
        con.Open()

        reader = cmd.ExecuteReader()


        While reader.Read()

            lbltitle.Text = reader(5)
            lblinfo.Text = reader(4) & " • " & reader(6) & " • " & reader(7) & " • "
            '10 desciption
            LabelVendor = reader(4)
            type = reader(6)
            issued = reader(7)
            DEsc = reader(10)
            ref1 = reader(13)
            ref2 = reader(14)
            check1 = reader(15)
        End While

        


    End Sub

    Private Sub ViewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewToolStripMenuItem.Click

        Dim rand As New Random
        Dim intrand As Integer = rand.Next("111111", "999999")

        Dim filena = lbltitle.Text & "-" & intrand & ".pdf"

        Dim ftp As FtpWebRequest = CType(FtpWebRequest.Create(FtpFile), FtpWebRequest)
        'Console.WriteLine("Downloading: " & FtpFile)
        ftp.Credentials = New NetworkCredential(My.Settings.ftpusername, My.Settings.ftppassword)
        ftp.KeepAlive = False
        ftp.UseBinary = True
        ftp.Method = WebRequestMethods.Ftp.DownloadFile
        Using FtpResponse As FtpWebResponse = CType(ftp.GetResponse, FtpWebResponse)
            Using ResponseStream As IO.Stream = FtpResponse.GetResponseStream

                Using fs As New IO.FileStream(filena, FileMode.Create)
                    Dim buffer(2047) As Byte
                    Dim read As Integer = 0
                    Do
                        read = ResponseStream.Read(buffer, 0, buffer.Length)
                        fs.Write(buffer, 0, read)
                        Console.Write(".")
                    Loop Until read = 0
                    ResponseStream.Close()
                    fs.Flush()
                    fs.Close()
                End Using
                ResponseStream.Close()
                MainForm.ViewedItem.Add(filena)
            End Using
        End Using
        Try
            Shell("explorer " & filena, AppWinStyle.MaximizedFocus, True, 1000)
        Catch ex As Exception
        End Try
    End Sub

End Class
